package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.ScoreGoodsService;
import com.baichang.taoshunda.service.ScoreListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu} on 2017/8/21 0021.
 */
@Controller
@RequestMapping(ControllerConstants.SCOREGOOD)
public class ScoreGoodsMCtronller extends BaseMobileController {
    @Autowired
    private ScoreGoodsService scoreGoodsService;
    @Autowired
    private ScoreListService scoreListService;

    /**
     * 根据编号获取对应的积分商品
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getScoreGoodsById")
    public void getScoreGoodsById(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer id = getInteger(json, "goodsId", 0);
                return scoreGoodsService.getScoreGoodsById(id);
            }
        });
    }

    /**
     * 兑换积分商品
     */
    @RequestMapping("/convertScoreGoods")
    public void convertScoreGoods(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer id = getInteger(json, "goodsId", 0);
                Integer userId = getInteger(json, "userId", 0);
                Integer goodsNumber = getInteger(json, "number", 1);
                Integer score = getInteger(json, "score", 0);
                String address = getString(json, "address", "");
                String scoreGoodsName = getString(json, "scoreGoodsName", "");
                String addressName = getString(json, "addressName", "");
                String addressUserName = getString(json, "addressUserName", "");
                String addressUserPhone = getString(json, "addressUserPhone", "");
                return scoreListService.convertScoreGoods(id, userId, score, address, goodsNumber, scoreGoodsName, addressName, addressUserName, addressUserPhone);
            }
        });
    }

    /**
     * 兑换积分商品的历史记录
     */
    @RequestMapping("/convertList")
    public void convertList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {

                Integer userId = getInteger(json, "userId", 0);
                Integer nowPage = getInteger(json, "nowPage", 1);
                Integer pageSize = getInteger(json, "pageSize", 20);

                return scoreListService.convertList(userId, nowPage, pageSize);
            }
        });
    }
}
